import random
import requests
from .settings import comment_list, headers, cookieApi


class CSDNScript:
    def __init__(self, url):
        self.cookieApi = cookieApi

        self.headers = headers

        # 文章链接
        self.article_id = url.split('/')[-1]

        self._init_cookies()
        self.next_cookie = self._build_next_cookie()

    def _init_cookies(self):
        """请求接口,获取全部的cookie"""
        self.cookies = requests.get(self.cookieApi).json()

    def _build_next_cookie(self):
        """cookie 生成器"""
        for k, v in self.cookies.items():
            yield (k, v)

    def _check_cookie_status(self):
        """检查cookie状态"""
        url = 'https://passport.csdn.net/v1/api/check/userstatus'
        response = requests.post(url, headers=self.headers).json()
        print(response)

    def _like_article(self, cookie):
        """点赞脚本"""
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36',
            'x-requested-with': 'XMLHttpRequest',
            'origin': 'https://blog.csdn.net',
            'cookie': cookie
        }

        data = {
            'articleId': self.article_id
        }
        response = requests.post(url='https://blog.csdn.net/phoenix/web/v1/article/like', headers=headers,
                                 data=data, timeout=8)
        # print('点赞', response.json())
        if not response.json()['data']['status']:
            response = requests.post(url='https://blog.csdn.net//phoenix/web/v1/article/like', headers=headers,
                                     data=data, timeout=8)

        return True

    def _comment_article(self, cookie):
        """评论脚本"""
        url = 'https://blog.csdn.net/phoenix/web/v1/comment/submit'

        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36',
            'x-requested-with': 'XMLHttpRequest',
            'origin': 'https://blog.csdn.net',
            'cookie': cookie
        }

        data = {
            "commentId": "",
            "content": random.choice(comment_list),
            "articleId": self.article_id
        }
        response = requests.post(url, headers=headers, data=data, timeout=8)
        # print(response.json())
        if response.status_code == requests.codes.ok:
            return True
        else:
            return False

    def start(self):
        """启动任务"""
        while True:
            try:
                cookie = self.next_cookie.__next__()
            except StopIteration:
                break

            # self.check_cookie_status()

            # print(cookie[0])
            like_status = self._like_article(cookie[1])
            comment_status = self._comment_article(cookie[1])
            print('点赞', like_status)
            print('评论', comment_status)

            if like_status and comment_status:
                print(cookie[0], '执行完毕!')


def gevent_start(article_url):
    csdn = CSDNScript(article_url)
    csdn.start()


# gevent_start('https://blog.csdn.net/weixin_50022322/article/details/110465651')
